package com.xindaoapp.happypet.ui;

import java.util.List;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.GoodsRecord;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 牛牛币订单
 * 
 * @author Administrator
 * 
 */
public class NiuniuOrderActivity extends BaseActivity {


    private RelativeLayout mNoData, mLeft;
    private PullToRefreshListView mPullToRefreshListView;

    private TextView vTitle, vLeft;


    @Override
    protected int getContentViewResId() {
        return R.layout.fragment_niuniuorder;
    }

    private final OnRefreshListener<ListView> mOnRefreshListener =
            new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    onLoadDatas();
                }
            };


    @Override
    protected void initViews() {
        super.initViews();
        setTopBarTitle("牛牛币订单");
        mNoData = (RelativeLayout) findViewById(R.id.no_data);
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(mOnRefreshListener);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getGoodsRecord(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<List<GoodsRecord>>() {
                    @Override
                    public void request(List<GoodsRecord> t) {
                        if (t != null) {
                            // mGoodsRecord = t;
                            mNoData.setVisibility(View.GONE);

                            mPullToRefreshListView.setAdapter(new MyAdapter(t));
                            onDataArrived(true);
                        } else {
                            mNoData.setVisibility(View.VISIBLE);
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });
    }

    class MyAdapter extends BaseAdapter {

        List<GoodsRecord> list = null;

        public MyAdapter(List<GoodsRecord> goods) {
            super();
            this.list = goods;
        }

        @Override
        public int getCount() {

            return list.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {

            ViewHolder holder;

            if (contentView == null) {
                contentView =
                        View.inflate(NiuniuOrderActivity.this, R.layout.item_myrecord_duihuan, null);
                holder = new ViewHolder();
                // holder.tv_line = (TextView) contentView.findViewById(R.id.tv_line);
                holder.icon = (ImageView) contentView.findViewById(R.id.icon);
                holder.tv_name = (TextView) contentView.findViewById(R.id.tv_name);
                holder.tv_jifen = (TextView) contentView.findViewById(R.id.tv_jifen);
                holder.tv_time = (TextView) contentView.findViewById(R.id.tv_time);
                holder.tv_status = (TextView) contentView.findViewById(R.id.tv_status);
                contentView.setTag(holder);
            } else {
                holder = (ViewHolder) contentView.getTag();
                // holder.iv_good.setImageResource(R.drawable.actionbar_back_indicator);
            }

            // if (position == 0) {
            // holder.tv_line.setVisibility(View.VISIBLE);
            // }

            GoodsRecord goods = list.get(position);
            ImageLoader.getInstance().displayImage(goods.goods_thumb, holder.icon);
            holder.tv_name.setText(goods.goods_name);
            holder.tv_jifen.setText(goods.exchange_integral + "尾巴币");
            holder.tv_time.setText(goods.updatetime);


            holder.tv_status.setText(goods.exchangetitle);


            return contentView;
        }

        class ViewHolder {
            ImageView icon;
            TextView tv_jifen, tv_status, tv_name, tv_time, tv_line;

        }

    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(1);
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftTextViewRes() {
        return R.string.niuniu_orderleft;
    }



}
